/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mujmail;

import org.apache.sanselan.formats.tiff.*;
import org.apache.sanselan.formats.jpeg.exifRewrite.*;
import org.apache.sanselan.formats.tiff.write.*;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.ImageWriteException;
import org.apache.sanselan.common.*;
import org.apache.sanselan.formats.jpeg.*;
import org.apache.sanselan.formats.tiff.constants.TiffConstants;

import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import javax.microedition.location.LocationException;
/**
 *
 * @author anup.s
 */
public class GPSStuff implements LocationListener {
final int MAX_GPS_ACTIVE_TIME=15*60*1000;//15 min in milisecond
LocationProvider lp;
    public double getLongi(){
        lastUsed=System.currentTimeMillis();
        return longi;
    }
    public double getLati(){
        lastUsed=System.currentTimeMillis();
        return lati;
    }
    private int gpsState ;
    double longi,lati;
    public static GPSStuff getInstance() throws LocationException{
        if(instance==null)
            instance = new GPSStuff();
        return instance;
    }
    private GPSStuff() throws LocationException {
        lp = LocationProvider.getInstance(null);
        lp.setLocationListener(this, -1, -1, -1);
    }
    private static boolean shutdownGPS;
    private long lastUsed;
    static void shutDownWhenNoNeed(boolean shutdown){
        shutdownGPS=shutdown;
    }
    private static GPSStuff instance ;
    public void locationUpdated(LocationProvider lp, Location location) {
        if (shutdownGPS && System.currentTimeMillis() - lastUsed > MAX_GPS_ACTIVE_TIME)//no one is using
        {
            switchOffGPS();
            return;
        }
        if ((gpsState = lp.getState()) == LocationProvider.AVAILABLE) {
            QualifiedCoordinates q = location.getQualifiedCoordinates();
            longi = q.getLongitude();
            lati = q.getLatitude();

        }
    }
    public static void switchOffGPS() {
        if (instance != null && instance.lp!=null ) {
            instance.lp.setLocationListener(null, -1, -1, -1);
            instance.lp = null;
            instance = null;
        }
        System.gc();
    }
    public void providerStateChanged(LocationProvider lp, int i) {
          if(callBack!=null)
             callBack.callBack(lp.getState());

    }
  
    public boolean isGPSAvailable(){
        lastUsed=System.currentTimeMillis();
        return gpsState==LocationProvider.AVAILABLE;
    }
    public GPSCallBack callBack;
    public void setGPSCallBack(GPSCallBack cb){
        callBack=cb;
    }
    public interface GPSCallBack {

        void callBack(int state);
    }
    
    /*
    public static void setGPS(double longitude, double latitude, float altitude, TiffOutputSet oset)
            throws ImageWriteException {
        TiffOutputDirectory gpsDirectory = oset.getOrCreateGPSDirectory();

        String longitudeRef = longitude < 0 ? "W" : "E";
        longitude = Math.abs(longitude);
        String latitudeRef = latitude < 0 ? "S" : "N";
        latitude = Math.abs(latitude);
        //String altitudeRef = altitude < 0 ? "S" : "N";
        //latitude = Math.abs(altitude);
        {
            TiffOutputField longitudeRefField = TiffOutputField.create(
                    TiffConstants.GPS_TAG_GPS_LONGITUDE_REF, oset.byteOrder,
                    longitudeRef);
            gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
            gpsDirectory.add(longitudeRefField);
        }

        {
            TiffOutputField latitudeRefField = TiffOutputField.create(
                    TiffConstants.GPS_TAG_GPS_LATITUDE_REF, oset.byteOrder,
                    latitudeRef);
            gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
            gpsDirectory.add(latitudeRefField);
        }
          {
        TiffOutputField altitudeRefField = TiffOutputField.create(
        TiffConstants.GPS_TAG_GPS_ALTITUDE_REF, oset.byteOrder,
        GPSTagConstants.GPS_TAG_GPS_ALTITUDE_REF_VALUE_ABOVE_SEA_LEVEL);
        gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_ALTITUDE_REF);
        gpsDirectory.add(altitudeRefField);
        }
        {
            double value = longitude;
            double longitudeDegrees = (long) value;
            value %= 1;
            value *= 60.0;
            double longitudeMinutes = (long) value;
            value %= 1;
            value *= 60.0;
            double longitudeSeconds = value;
            Double values[] = {
                new Double(longitudeDegrees), new Double(longitudeMinutes),
                new Double(longitudeSeconds),};

            TiffOutputField longitudeField = TiffOutputField.create(
                    TiffConstants.GPS_TAG_GPS_LONGITUDE, oset.byteOrder, values);
            gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_LONGITUDE);
            gpsDirectory.add(longitudeField);
        }

        {
            double value = latitude;
            double latitudeDegrees = (long) value;
            value %= 1;
            value *= 60.0;
            double latitudeMinutes = (long) value;
            value %= 1;
            value *= 60.0;
            double latitudeSeconds = value;
            Double values[] = {
                new Double(latitudeDegrees), new Double(latitudeMinutes),
                new Double(latitudeSeconds),};

            TiffOutputField latitudeField = TiffOutputField.create(
                    TiffConstants.GPS_TAG_GPS_LATITUDE, oset.byteOrder, values);
            gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_LATITUDE);
            gpsDirectory.add(latitudeField);
        }
        {
            float value = altitude;
            Double values[] = {
                new Double(value)
            };

            TiffOutputField altitudeField = TiffOutputField.create(
                    TiffConstants.GPS_TAG_GPS_ALTITUDE, oset.byteOrder, values);
            gpsDirectory.removeField(TiffConstants.GPS_TAG_GPS_ALTITUDE);
            gpsDirectory.add(altitudeField);
        }

    }*/
}
